package com.gitee.wsl.common.chart.render.foreground.info

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import com.gitee.wsl.common.ui.draw.type.HorizontalAlign
import com.gitee.wsl.common.ui.draw.type.VerticalAlign
import com.gitee.wsl.android.ui.ext.type.drawMultilineText
import com.gitee.wsl.android.ui.ext.type.textHeight
import kotlin.math.abs
import kotlin.math.roundToInt

/**
* @Description 标题类,定制其属性
* @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
*
*/
class Title {

    var isVisable = true

    //图表标题文字
     var title = ""
     var subTitle = ""

    //图表标题画笔
     val titlePaint: Paint by lazy {
        Paint().apply {
            textSize = 32f
            color = Color.BLACK
            isAntiAlias = true
        }
    }
     val subtitlePaint: Paint  by lazy {
         Paint().apply {
             textSize = 22f
             color = Color.BLACK
             isAntiAlias = true
         }
     }

    //图表标题显示位置
     var titleHorizontalAlign = HorizontalAlign.CENTER

    //标题的显示位置(TOP,CENTER,BOTTOM)即是否靠最上面，
    //还是Chart top与Plot top的中间位置，还是PLOT TOP的位置
     var titleVerticalAlign = VerticalAlign.MIDDLE

    /**
     * 绘制标题
     */
    fun renderTitle(
        chartLeft: Float,
        chartRight: Float,
        chartTop: Float,
        chartWidth: Float,
        plotTop: Float,
        canvas: Canvas
    ) {

        if(!isVisable) return

        //排除掉border width
        val title: String = title
        val subTitle: String = subTitle
        var titleHeight = 0f
        var subtitleHeight = 0f
        var totalHeight = 0f
        var titleInitY = 0.0f
        var titleX = 0.0f
        var titleY = 0.0f
        var subtitleX = 0.0f
        var subtitleY = 0.0f
        if (title.isEmpty() && subTitle.isEmpty()) return
        if (title.isNotEmpty()) {
            titleHeight = titlePaint.textHeight
        }
        if (title.isNotEmpty()) {
            subtitleHeight = subtitlePaint.textHeight
        }
        totalHeight = titleHeight + subtitleHeight
        val pcHeight = abs(plotTop - chartTop)
        when (titleVerticalAlign) {
            VerticalAlign.TOP -> titleInitY = chartTop + titleHeight
            VerticalAlign.MIDDLE -> titleInitY = (chartTop + pcHeight / 2 - totalHeight / 2).roundToInt().toFloat()
            VerticalAlign.BOTTOM -> titleInitY = plotTop - titleHeight
        }
        when (titleHorizontalAlign) {
            HorizontalAlign.LEFT -> {
                titleX = chartLeft
                titleY = titleInitY
                subtitleX = chartLeft
                subtitleY = titleY + subtitleHeight
                titlePaint.textAlign = Paint.Align.LEFT
                subtitlePaint.textAlign = Paint.Align.LEFT
            }
            HorizontalAlign.CENTER -> {
                titleX = (chartLeft + chartWidth / 2).roundToInt().toFloat()
                titleY = titleInitY
                titlePaint.textAlign = Paint.Align.CENTER
                subtitlePaint.textAlign = Paint.Align.CENTER
            }
            HorizontalAlign.RIGHT -> {
                titleX = chartRight
                titleY = titleInitY
                titlePaint.textAlign = Paint.Align.RIGHT
                subtitlePaint.textAlign = Paint.Align.RIGHT
            }
        }

        subtitleY = canvas.drawMultilineText(titlePaint, title, titleX, titleY)
        subtitleX = titleX
        subtitleY = titleY + subtitleHeight
        canvas.drawMultilineText(subtitlePaint, subTitle, subtitleX, subtitleY)
    }
    
}